package com.study.newjava8.date;


import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;
import java.util.logging.Level;

/**
 * 使用时间API
 * @author huangsm
 * @version V1.0
 */
public class TestLocalDateTime {
    /**
     *1.LocalDate LocalTime LocalDateTime
     */
    @Test
    public void test1(){
        LocalDateTime localDateTime=LocalDateTime.now();
        System.out.println(localDateTime);

        //自定义时间
        LocalDateTime of = LocalDateTime.of(2015, 10, 19, 13, 22, 33);
        System.out.println(of);

        //时间运算 加10天，其他类似
        LocalDateTime localDateTime1 = localDateTime.plusDays(10);
        System.out.println(localDateTime1);

        //加2个月
        LocalDateTime localDateTime2 = localDateTime.minusMonths(2);
        System.out.println(localDateTime2);

        //得到年月或者月对象
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getSecond());
    }

    /**
     * 2. Instant:时间戳(以Unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)
     */
    @Test
    public void test2(){
        //默认获取UTC时区 世界协调时间
        Instant ins1 = Instant.now();
        System.out.println(ins1);
        //偏移8小时
        OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(odt);
        System.out.println(ins1.toEpochMilli());

        Instant instant = Instant.ofEpochSecond(1000);
        System.out.println(instant);
    }
    /**
     * 3.Duration:计算"时间"之间的间隔
     *   Period: 计算两个"日期"之间的间隔
     **/

    @Test
    public void test3(){
        Instant ins1=Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant ins2 = Instant.now();
        System.out.println(Duration.between(ins1,ins2).toMillis());

        System.out.println("----------------------------------");
        //两个时间间的间隔
        LocalTime lt1=LocalTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime lt2=LocalTime.now();
        System.out.println(Duration.between(lt1,lt2).toMillis());
    }

    @Test
    public void test4(){
        LocalDate ld1=LocalDate.now();
        LocalDate ld2=LocalDate.of(2015,1,1);
        int days = Period.between(ld2, ld1).getDays();
        System.out.println(days);
    }
    /**
     * - TemporalAdjuster : 时间校正器。有时我们可能需要获取例如：将日期调整到“下个周日”等操作。
     * - TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。
     */
    @Test
    public void test5(){
        LocalDateTime ldt=LocalDateTime.now();
        System.out.println(ldt);
        //将月中的日指定为10
        LocalDateTime ldt2 = ldt.withDayOfMonth(10);
        System.out.println(ldt2);

        //下一个周五
        LocalDateTime ldt3= ldt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        System.out.println(ldt3);

        //自定义:下一个工作日
        LocalDateTime ldt5 = ldt.with((l) -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            //获取是周几
            DayOfWeek dayOfWeek = ldt4.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                //加三天
                return ldt4.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);
            } else {
                return ldt4.plusDays(1);
            }

        });
        System.out.println(ldt5);
    }

    /**
     * DateTimeFormatter:格式化时间或者是日期
     */
    @Test
    public void test6(){
        DateTimeFormatter dtf=DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime ldt = LocalDateTime.now();
        String format = ldt.format(dtf);
        System.out.println(format);
        System.out.println("-----------");
        //自定义
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format1 = ldt.format(dateTimeFormatter);
        System.out.println(format1);
        //LocalDateTime hh = ldt.parse(format,dateTimeFormatter);
        //System.out.println(hh);
    }

    /**
     * ZonedDate、ZonedTime、ZonedDateTime
     */
    @Test
    public void test7(){
        //支持的时区
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);
    }

    @Test
    public void test8(){
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Europe/Monaco"));
        System.out.println(now);


        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("US/Pacific"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("US/Pacific"));
        System.out.println(zdt );

    }
}
